<template>
  <rx-dialog @handOk="onOk" @cancel="onCancel" >
    <rx-layout>
      <div slot="header" height="80">
        <p>

            {{i18n('list.commFunc','常用函数')}}
          <a-select @change="funChange" :options="codeTemplate" style="width:200px;"  allowClear>

          </a-select>
          <a-button @click="addFormJson">{{i18n('list.formData','表单数据')}}</a-button>
        </p>
      </div>
      <div slot="center">
        <rx-monaco-editor
          ref="scriptCode"
          v-model="template"
          language="javascript"
          style="height:300px;"
        >
        </rx-monaco-editor>
      </div>
    </rx-layout>
  </rx-dialog>
</template>

<script>
    import {RxLayout,RxDialog,Util,RxMonacoEditor} from "@lowcode/jpaas-common-lib"
    import FormBoAttrApi from "@/api/form/bo/formBoAttr";
    import codeTemplate from "@/views/modules/form/core/js-comps/codeTemplate";

    export default {
      name: "ButtonFunConfig",
      components: {
        RxLayout, RxDialog,RxMonacoEditor
      },
      props:{
        action:String,
        boDefId:String,
        layerid: String,
        destroy:Function
      },
      data(){
        return {
          template:"",

          codeTemplate:codeTemplate
        }
      },
      created() {
        if(this.action){
          this.template=this.action;
        }
      },
      methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"formBoAttr",key);
        },
        onOk(){
          Util.closeWindow(this,'ok',{action:this.template});
        },
        onCancel(){
          Util.closeWindow(this,'cancel');
        },
        funChange(value){
            this.$refs.scriptCode.insertContent(value);
        },
        addFormJson(){
          var self_ = this;
          var config = {
            curVm: self_, widthHeight: ['800px', '600px']
          };
          if (!this.boDefId) {
            this.$confirm({
              title: self_.$ti18('rxMsa.masWarn.delInfo','警告'),
              content: self_.i18n('msa.addFormJsonTips','业务模型为空'),
              cancelText: self_.$ti18('rxForm.cancelBtn','取消')
            });
            return;
          }
          Object.assign(config, {
            data: {
              boDefId: this.boDefId
            }
          });
          FormBoAttrApi.openBoAttrDialog(config, function (data) {
            if(data.type=='main'){
              for(var i=0;i<data.returnArray.length;i++){
                var field=data.returnArray[i];
                var f="var "+field.field+" =formJson['"+field.field+"'];\n";
                self_.$refs.scriptCode.insertContent(f);
              }
            }else if(data.type=='onetomany') {
              var f = "for(var i=0;formJson['sub__" + data.entName + "'].length;i++){\n";
              for (var i = 0; i < data.returnArray.length; i++) {
                var field = data.returnArray[i];
                f+="var "+field.field+" =formJson['sub__" + data.entName + "'][i]['"+field.field+"'];\n";
              }
              f+="}\n";
              self_.$refs.scriptCode.insertContent(f);
            }else if(data.type=='onetoone'){
              for(var i=0;i<data.returnArray.length;i++){
                var field=data.returnArray[i];
                var f="var "+field.field+" =formJson['sub__"+data.entName+"']['"+field.field+"'];\n";
                self_.$refs.scriptCode.insertContent(f);
              }
            }
          });
        }
      }
    }
</script>

<style scoped>

</style>